﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBConnectionLib;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    [Ignore]
    public class TestSourceDatabaseWriter
    {
        [Test]
        public void TestWriteTarget()
        {
            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(BaseDirectoryHelper.TestFiles_output);

            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                BaseDirectoryHelper.TestFiles_output, "DBSourceToolsTestDB");

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDBSourceToolsTestDB());

            oDBProject.SourceDatabases[0].AddTargetDatabase( oStub.GetDeployDBSourceToolsTestDB(), true);
            oDBProject.CreateBaseDirectories();

            SourceDatabase oSource = oDBProject.SourceDatabases[0];

            System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();

            DBSourceToolsLib.SourceDatabaseLoader oSourceLoader =
                new DBSourceToolsLib.SourceDatabaseLoader(oForm,
                    DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDatabase);

            oDBProject.WriteProjectFile();

            oSourceLoader.Execute(ref oSource, null);
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Table)
                , 1, "Should load 1 table");
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.View)
                , 1, "Should load 1 view");
            Assert.AreEqual(oSource.SysObjects.Count(DBSourceToolsLib.SysObjects.SysObjectType.Function)
                , 1, "Should load 1 function");

            oSource.ScriptOptions.ScriptData = false;
            oSource.ScriptOptions.ScriptDropAndCreate = true;
            oSource.ScriptOptions.ScriptStoredProcs = true;
            oSource.ScriptOptions.ScriptTables = true;
            oSource.ScriptOptions.ScriptUsers = true;
            oSource.ScriptOptions.ScriptViews = true;

            string strScriptOptionsDir = BaseDirectoryHelper.TestFiles_control + "\\DBSourceToolsTestDB";

            oSource.ScriptOptions.LoadTableScriptOptionsFile(strScriptOptionsDir + "\\TableScriptOptions.xml", null);
            Assert.IsTrue(oSource.ScriptOptions.MustScriptTable("TestTable_1"), "Must script table TestTable_1 should be true");

            oSource.ScriptOptions.LoadViewOptionsFile(strScriptOptionsDir + "\\ViewScriptOptions.xml", null);
            Assert.IsTrue(oSource.ScriptOptions.MustScriptView("TestView_1"), "Must script view TestView_1 should be true");
            Assert.IsTrue(oSource.ScriptOptions.MustScriptData("TestTable_1"), "Must script data TestView_1 should be true");

            oSource.ScriptOptions.LoadProcsOptionsFile(strScriptOptionsDir + "\\StoredProcScriptOptions.xml", null);
            Assert.IsTrue(oSource.ScriptOptions.MustScriptProc("TestProc"), "Must script proc TestProc should be true");

            // must set the startup path 
            UserSettings.Instance().StartupPath = BaseDirectoryHelper.StartupPath;

            DBSourceToolsLib.SourceDatabaseWriter oWriter = new SourceDatabaseWriter(oForm,
                DBSourceToolsLib.Commands.DatabaseCommandTypes.WriteToDisk, oDBProject);
            oWriter.Execute(ref oSource, null);

            string strControlBatchFile = BaseDirectoryHelper.TestFiles_control
                + "\\DBSourceToolsTestDB\\DeploymentTargets\\DeployDBSourceToolsTestDB\\Run_Create_Scripts.bat";
            string strTargetBatchFile = BaseDirectoryHelper.TestFiles_output
                + "\\DBSourceToolsTestDB\\DeploymentTargets\\DeployDBSourceToolsTestDB\\Run_Create_Scripts.bat";

            oFileHelper.CompareFiles(strControlBatchFile, strTargetBatchFile);
        }

        [Test]
        public void TestDeleteDeploymentTarget()
        {
            FileHelper oFileHelper = new FileHelper();
            oFileHelper.DeleteFilesInDir(BaseDirectoryHelper.TestFiles_output);

            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(
                BaseDirectoryHelper.TestFiles_output, "DBSourceToolsTestDB");

            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDBSourceToolsTestDB());

            Database oTargetDatabase = oStub.GetDeployDBSourceToolsTestDB();
            oDBProject.SourceDatabases[0].AddTargetDatabase(oTargetDatabase, true);
            oDBProject.CreateBaseDirectories();

            SourceDatabase oSource = oDBProject.SourceDatabases[0];

            System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();

            DBSourceToolsLib.SourceDatabaseLoader oSourceLoader =
                new DBSourceToolsLib.SourceDatabaseLoader(oForm,
                    DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDatabase);

            oDBProject.WriteProjectFile();
            oSourceLoader.Execute(ref oSource, null);

            oSource.ScriptOptions.ScriptData = false;
            oSource.ScriptOptions.ScriptDropAndCreate = true;
            oSource.ScriptOptions.ScriptStoredProcs = true;
            oSource.ScriptOptions.ScriptTables = true;
            oSource.ScriptOptions.ScriptUsers = true;
            oSource.ScriptOptions.ScriptViews = true;

            string strScriptOptionsDir = BaseDirectoryHelper.TestFiles_control + "\\DBSourceToolsTestDB";

            oSource.ScriptOptions.LoadTableScriptOptionsFile(strScriptOptionsDir + "\\TableScriptOptions.xml", null);
            oSource.ScriptOptions.LoadViewOptionsFile(strScriptOptionsDir + "\\ViewScriptOptions.xml", null);
            oSource.ScriptOptions.LoadProcsOptionsFile(strScriptOptionsDir + "\\StoredProcScriptOptions.xml", null);

            // must set the startup path 
            UserSettings.Instance().StartupPath = BaseDirectoryHelper.StartupPath;

            DBSourceToolsLib.SourceDatabaseWriter oWriter = new SourceDatabaseWriter(oForm,
                DBSourceToolsLib.Commands.DatabaseCommandTypes.WriteToDisk, oDBProject);
            oWriter.Execute(ref oSource, null);

            string strControlBatchFile = BaseDirectoryHelper.TestFiles_control
                + "\\DBSourceToolsTestDB\\DeploymentTargets\\DeployDBSourceToolsTestDB\\Run_Create_Scripts.bat";
            string strTargetBatchFile = BaseDirectoryHelper.TestFiles_output
                + "\\DBSourceToolsTestDB\\DeploymentTargets\\DeployDBSourceToolsTestDB\\Run_Create_Scripts.bat";

            oFileHelper.CompareFiles(strControlBatchFile, strTargetBatchFile);

            oSource.RemoveTargetDatabase(oTargetDatabase.NickName);
            string strTargetDirectoryBaseDir = BaseDirectoryHelper.TestFiles_output
                + "\\DBSourceToolsTestDB\\DeploymentTargets\\DeployDBSourceToolsTestDB\\";
            Assert.IsFalse(System.IO.Directory.Exists(strTargetDirectoryBaseDir), strTargetDirectoryBaseDir + " should be removed");
            
        }

    }
}
